import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class FormTestRoute extends StatefulWidget {
  const FormTestRoute({Key? key}) : super(key: key);

  @override
  _FormTestRouteState createState() => _FormTestRouteState();
}

class _FormTestRouteState extends State<FormTestRoute> {
  final TextEditingController _unameController = TextEditingController();
  final TextEditingController _pwdController = TextEditingController();
  final GlobalKey _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("表单"),
      ),
      body: Form(
          key: _formKey,
          autovalidateMode: AutovalidateMode.onUserInteraction,
          child: Container(
            padding: EdgeInsets.all(20),
            child: Column(
              children: <Widget>[
                TextFormField(
                  autofocus: true,
                  controller: _unameController,
                  decoration: const InputDecoration(
                    labelText: '用户名',
                    hintText: '用户名或邮箱',
                    icon: Icon(Icons.person),
                  ),
                  validator: (v) {
                    return v!.trim().isNotEmpty ? null : '用户名或邮箱';
                  },
                ),
                TextFormField(
                  autofocus: false,
                  controller: _pwdController,
                  decoration: const InputDecoration(
                    labelText: '密码',
                    hintText: '您的登录密码',
                    icon: Icon(Icons.lock),
                  ),
                  validator: (v) {
                    return v!.trim().length > 5 ? null : '密码不能少于6位';
                  },
                ),
                Padding(
                  padding: EdgeInsets.only(top: 30),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          child: ElevatedButton(
                        child: const Padding(
                            padding: EdgeInsets.all(16.0), child: Text("登录")),
                        onPressed: () {
                          if ((_formKey.currentState as FormState).validate()) {
                            print('验证通过');
                          }
                        },
                      )),
                    ],
                  ),
                ),
              ],
            ),
          )),
    );
  }
}
